<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>音乐播放器</title>
    <style>
        .active {
            background-color: #42b983;
        }
    </style>
</head>
<body>
<!--
自动播放



-->
    <div id="app">
<!--
controls 显示控件
-->
<!--        <audio src="../musics/11.mp3" controls autoplay ></audio>-->
<!--        <audio :src="songList[currentIndex].src" controls autoplay @ended="handleStop"></audio>-->
<!--        使用计算属性,将较为复杂的计算放到下面-->
        <audio :src="songSrc" controls autoplay @ended="handleStop"></audio>
        <ul>
            <li
                    v-for="(item, index) in songList"
                    :key="index"
                    :class="{active:index==currentIndex}"
                    @click="playSong(index)"
            >
                <span>名称: {{item.name}}</span>
            </li>
        </ul>
    </div>
</body>
<script src="../js/vue.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data(){
            return {
                songList: [
                    {
                        name: '11',
                        src: '../musics/11.mp3',
                    },
                    {
                        name: '22',
                        src: '../musics/22.mp3',
                    },
                    {
                        name: '天下',
                        src: '../musics/天下.mp3',
                    },
                    {
                        name: '断点',
                        src: '../musics/断点.mp3',
                    }
                ],
                currentIndex: 0
            }
        },
        created(){

        },
        computed: {  // 定义计算属性
            songSrc(){
                return this.songList[this.currentIndex].src
            }
        },
        methods: {  // 定义事件
            playSong(index){
                this.currentIndex = index
            },
            handleStop(){
                // alert('当前歌曲已经停止')
                // 自动下一首
                if (this.currentIndex === this.songList.length-1) {
                    this.currentIndex = 0;
                }else {
                    this.currentIndex++
                }
            }
        }
    });
</script>
</html>